package com.example.mobiletest.ui.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import com.example.mobiletest.manager.BookingDataManager
import com.example.mobiletest.model.Booking
import kotlinx.coroutines.launch

class BookingViewModel(application: Application) : AndroidViewModel(application) {
    
    private val bookingDataManager = BookingDataManager(application)
    
    // 将StateFlow转换为LiveData，避免在Activity中使用StateFlow.collect()的问题
    val bookingData: LiveData<Booking?> = bookingDataManager.bookingData.asLiveData()
    val isLoading: LiveData<Boolean> = bookingDataManager.isLoading.asLiveData()
    val error: LiveData<String?> = bookingDataManager.error.asLiveData()
    
    /**
     * 加载预订数据
     */
    fun loadBookingData() {
        viewModelScope.launch {
            bookingDataManager.getBookingData()
        }
    }
    
    /**
     * 刷新预订数据
     */
    fun refreshBookingData() {
        viewModelScope.launch {
            bookingDataManager.refreshBookingData()
        }
    }
    
    /**
     * 清除所有数据
     */
    fun clearData() {
        bookingDataManager.clearAllData()
    }
}